package team18.yuejian.Activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;

import team18.yuejian.Adapter.ReviewAdapter;
import team18.yuejian.Adapter.ShowPicAdapter;
import team18.yuejian.Model.RetrofitModel;
import team18.yuejian.Model.ShowPicModel;
import team18.yuejian.Other.CustomDialog;
import team18.yuejian.R;
import team18.yuejian.RetrofitListener;
import team18.yuejian.bean.NoteBean;
import team18.yuejian.bean.PicDeteilBean;
import team18.yuejian.bean.ReviewBean;

public class PicDetailActivity extends BaseActivity implements View.OnClickListener,RetrofitListener {


    private TextView username,time,intro;
    private ImageView back,layoutPic;
    private FloatingActionButton addReview;
    private RecyclerView recyclerView;
    private ReviewAdapter adapter;
    private EditText editText_review;
    private int spid;

    private Object object;
    private List<ReviewBean> beans,mmlist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_detail);
        initView();
        initData();
        initEvent();

    }

    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==3){
            /*o = new PicDeteilBean();*/
            if (((PicDeteilBean) o).getSpid()==spid){
                Toast.makeText(PicDetailActivity.this,((PicDeteilBean) o).getMsg(),Toast.LENGTH_SHORT).show();
                username.setText("上传人："+((PicDeteilBean) o).getUname());
                time.setText("上传时间："+((PicDeteilBean) o).getSptime());
                intro.setText("图片简介："+((PicDeteilBean) o).getSpcontent());

                RetrofitModel retrofitModel = new RetrofitModel();
                String url = retrofitModel.getPicUrl();

                Glide.with(PicDetailActivity.this).load(url+"/showpic/"+((PicDeteilBean) o).getSppic())
                        .into(layoutPic);

            }else{
                Toast.makeText(PicDetailActivity.this,"获取详情失败",Toast.LENGTH_SHORT).show();
            }
        }else if (flag==4){
            mmlist = (List<ReviewBean>) o;
            GridLayoutManager layoutManager = new GridLayoutManager(this,1);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new ReviewAdapter(mmlist);
            recyclerView.setAdapter(adapter);
        }else if (flag==5){
            Toast.makeText(this,((NoteBean)o).getMsg(),Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(PicDetailActivity.this,"未知命令",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.PicDetail_back:
                /*Intent intent = new Intent(PicDetailActivity.this,ShowPicActivity.class);
                startActivity(intent);*/
                PicDetailActivity.this.finish();
                break;

            case R.id.PicDetail_add_content:
                setAlert();
                break;

        }

    }

    public void initView(){
        layoutPic = findViewById(R.id.PicDetail_pic);
        username = findViewById(R.id.AddPic_username);
        time = findViewById(R.id.AddPic_time);
        intro = findViewById(R.id.PicDetail_intro);
        back = findViewById(R.id.PicDetail_back);

        addReview =findViewById(R.id.PicDetail_add_content);
        recyclerView = findViewById(R.id.PicDetail_review_list);
    }

    public void initData(){
        Intent intent = getIntent();
        spid = intent.getExtras().getInt("spid");
        ShowPicModel model = new ShowPicModel();
        model.getPicDetail(spid,this);

        ShowPicModel model1 = new ShowPicModel();
        model1.getReview(spid,this);

    }

    public void initEvent(){
        back.setOnClickListener(this);
        addReview.setOnClickListener(this);

    }

    public void setAlert(){
        final CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setTitle("添加评论");
        final LinearLayout layout_alert = (LinearLayout) getLayoutInflater().inflate(R.layout.review_set_model,null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                int uid = getUserId();
                String review = builder.getContent();

                addReviewFuntion(uid,spid,review);

                Toast.makeText(PicDetailActivity.this,review,Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                Toast.makeText(PicDetailActivity.this,"取消",Toast.LENGTH_SHORT).show();
            }
        });

        builder.create().show();

    }

    public void addReviewFuntion(int uid,int spid, String conment){
        ShowPicModel model = new ShowPicModel();
        model.addReview(uid,spid,conment,this);
    }
}
